package com.hdd.fmh.common.response;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
* @Description:  在拦截器中 直接返回  json 数据 
* @author: coco 
* @date: 2019-10-29 10:43:46
 */
@Slf4j
public class ResultIoVo {
	
	  public static  void responseOut666(HttpServletResponse response,  Object obj) {
	    	  response.setCharacterEncoding("UTF-8");
	          response.setHeader("Content-Type", "application/json");
	          response.setHeader("Access-Control-Allow-Credentials", "true");
	          response.setHeader("Access-Control-Allow-Methods", "GET, POST");
	          response.setHeader("Access-Control-Allow-Origin", "*");
	          response.setHeader("Access-Control-Max-Age", "3600");       
	          response.setStatus(401);
	          response.setContentType("application/json;charset=UTF-8");
	          PrintWriter writer=null;
	          try {
	              writer=response.getWriter();
	              writer.write(JSONObject.toJSONString(obj));
	              writer.flush();
	          } catch (IOException ex) {
	        	  log.error(ex.getMessage());
	          }finally {
	              if(writer!=null) {
	                  writer.close();
	              }
	      }
	    }
	
    public static  void responseOutWithJson(HttpServletResponse response,
                                            Object obj) {
    	  response.setCharacterEncoding("UTF-8");
          response.setHeader("Content-Type", "application/json");
          response.setHeader("Access-Control-Allow-Credentials", "true");
          response.setHeader("Access-Control-Allow-Methods", "GET, POST");
          response.setHeader("Access-Control-Allow-Origin", "*");
          response.setHeader("Access-Control-Max-Age", "3600");       
          response.setStatus(401);
          response.setContentType("application/json;charset=UTF-8");
          PrintWriter writer=null;
          try {
              writer=response.getWriter();
              writer.write(JSON.toJSONString(obj));
              writer.flush();
          } catch (IOException ex) {
              log.error(ex.getMessage());
          }finally {
              if(writer!=null) {
                  writer.close();
              }
      }
    }
    
    
    public static  void responseOutWithJsonTwo(HttpServletResponse response,   Object obj) {
    	  response.setCharacterEncoding("UTF-8");
          response.setHeader("Content-Type", "application/json");
          response.setHeader("Access-Control-Allow-Credentials", "true");
          response.setHeader("Access-Control-Allow-Methods", "GET, POST");
          response.setHeader("Access-Control-Allow-Origin", "*");
          response.setHeader("Access-Control-Max-Age", "3600");       
          response.setContentType("application/json;charset=UTF-8");
          PrintWriter writer=null;
          try {
              writer=response.getWriter();
              writer.write(JSON.toJSONString(obj));
              writer.flush();
          } catch (IOException ex) {
              log.error(ex.getMessage());
          }finally {
              if(writer!=null) {
                  writer.close();
              }
      }
    } 
}
